package com.roast.services.my;

import com.roast.util.QueryObject;
import java.util.ArrayList;
import com.roast.domain.*;
import com.roast.util.*;

public class GetPublicEventsIMPL extends GeneralSO implements GetPublicEvents {

    private QueryObject queryObject;
    private ArrayList<DomainObject> fromDB;

    @Override
    public int getEvents(ArrayList<Event> list) {
        return new GetPublicEventsIMPL().generalExecution(list);
    }

    @Override
    protected int executeSO(Object o) {
        if (!setQuery()) {
            return 11001;
        }
        if (!readEvents()) {
            return 11002;
        }
        if (!copyList(o)) {
            return 11003;
        }
        return 11000;
    }

    private boolean setQuery() {
        queryObject = QueryBeanFactory.getBean("readPublicEvents");
        if (queryObject != null) {
            return queryObject.addParametar("fisprivate", false);
        } else {
            return false;
        }
    }

    private boolean readEvents() {
        try {
            fromDB = dbbr.readMany(queryObject);
        } catch (NullPointerException ex) {
            System.out.println("[GetPublicEvents:readEvents:[" + ex + "]]");
            return false;
        }
        return queryObject.getSignal() == 10040;
    }

    @SuppressWarnings("unchecked")
    private boolean copyList(Object o) {
        return CopyArrayList.events(fromDB, (ArrayList<Event>) o);
    }

    @Override
    protected boolean executeState(int signal) {
        switch (signal) {
            case 11000:
                showOperationState(signal, "_getPublicEvents: You have successfully get public events from database.");
                return true;
            case 11001:
                showOperationState(signal, "_getPublicEvents:ERROR: Fail to set event and query.");
                return false;
            case 11002:
                showOperationState(signal, "_getPublicEvents:ERROR: Fail to read events from database.");
                return false;
            case 11003:
                showOperationState(signal, "_getPublicEvents:ERROR: Fail to copy events.");
                return false;
        }
        return false;
    }
}
